package com.tulingxueyuan.ribbon.rule;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @description: 自定义负载均衡规则
 * @Author li
 * @Date 2025 四月 2025/4/22 10:48
 */
public class CustomRule extends AbstractLoadBalancerRule {
    @Override
    public Server choose(Object key) {
        // 获得当前请求的服务的示例
        ILoadBalancer loadBalancer = this.getLoadBalancer();
        List<Server> reachableServers = loadBalancer.getReachableServers();

        int random = ThreadLocalRandom.current().nextInt(reachableServers.size());

        return reachableServers.get(random);
    }

    @Override
    public void initWithNiwsConfig(IClientConfig iClientConfig) {

    }
}
